package com.webserver.controller;

import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;

public class UserController {
    private static File USER_DIR = new File("./users");

    static {
        if (!USER_DIR.exists()) {
            USER_DIR.mkdirs();
        }
    }

    public void reg(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理用户注册.....");
        //1.获取用户在表单上输入的信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        System.out.println(username + ", " + password + ", " + nickname + ", " + ageStr);

        int age = Integer.parseInt(ageStr);

        //2.将信息保存
        //用户名.obj
        User user = new User(username, password, nickname, age);

        File userFile = new File(USER_DIR, username + ".obj");
        try (
                FileOutputStream fos = new FileOutputStream(userFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            oos.writeObject(user);
            System.out.println("注册成功!");

            File file = new File("./webapps/myweb/reg_success.html");
            response.setContentFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }


        //3.响应客户端注册结果的页面

        System.out.println("用户注册处理完毕");

    }

    public void login(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理用户登录...");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //必要验证
        if (username == null || password == null) {
            File file = new File("./webapps/myweb/login_info_error.html");
            response.setContentFile(file);
            return;
        }
        //处理登录
        //定位到登录用户的文件
        File userFile = new File(USER_DIR, username + ".obj");
        if (userFile.exists()) {
            try (
                    ObjectInputStream ois = new ObjectInputStream(
                            new FileInputStream(userFile)
                    )
            ) {
                User user = (User) ois.readObject();
                if (user.getPassword().equals(password)) {
                    //登录成功
                    File file = new File("./webapps/myweb/login_success.html");
                    response.setContentFile(file);
                    return;
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        File file = new File("./webapps/myweb/login_fail.html");
        response.setContentFile(file);


        System.out.println("处理用户登录完毕!");
    }


//    public void login(HttpServletRequest request, HttpServletResponse response) {
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        try {
//            FileInputStream fis = new FileInputStream(USER_DIR + "/" + username + ".obj");
//            try {
//                ObjectInputStream ois = new ObjectInputStream(fis);
//                User user2 = (User) ois.readObject();
//                if (username == null || password == null) {
//                    File file = new File("./webapps/myweb/login_info_error.html");
//                    response.setContentFile(file);
//                    return;
//                }
//                if (password.equals(user2.getPassword())) {
//                    File file = new File("./webapps/myweb/login_success.html");
//                    response.setContentFile(file);
//                } else {
//                    File file = new File("./webapps/myweb/login_fail.html");
//                    response.setContentFile(file);
//                }
//            } catch (ClassNotFoundException | IOException e) {
//                e.printStackTrace();
//            }
//
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//    }
}
